---

# Default compose file for development and production.
# Should be used directly in development.
# Automatically loads `docker-compose.override.yml` if it exists.
# No extra steps required.
# Should be used together with `docker/docker-compose.prod.yml`
# in production.

version: "3.8"
services:
  db:
    image: "postgres:14-alpine"
    restart: unless-stopped
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - postgresnet
    env_file: ./config/.env

  web:
    <<: &web
      # Image name is changed in production:
      image: "blog-project:dev"
      build:
        target: development_build
        context: .
        dockerfile: ./docker/django/Dockerfile
        args:
          DJANGO_ENV: development
        cache_from:
          - "blog-project:dev"
          - "blog-project:latest"
          - "*"

      volumes:
        - django-static:/var/www/django/static
      depends_on:
        - db
      networks:
        - webnet
        - postgresnet
      env_file: ./config/.env
      environment:
        DJANGO_DATABASE_HOST: db

    command: python -Wd manage.py runserver 0.0.0.0:8000
    healthcheck:
      # We use `$$` here because:
      # one `$` goes to shell,
      # one `$` goes to `docker-compose.yml` escaping
      test: |
        /usr/bin/test $$(
          /usr/bin/curl --fail http://localhost:8000/health/?format=json
          --write-out "%{http_code}" --silent --output /dev/null
        ) -eq 200
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

# This task is an example of how to extend existing ones:
#   some_worker:
#     <<: *web
#     command: python manage.py worker_process

networks:
  # Network for postgres, use it for services that need access to the db:
  postgresnet:
  # Network for your internals, use it by default:
  webnet:

volumes:
  pgdata:
  django-static:
